package com.dingding.webservice.activity.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.dingding.bean.databean.request.ReqActivityInfo;
import com.dingding.bean.databean.request.ReqExchange;
import com.dingding.bean.databean.request.ReqGetIsAttendActivity;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResGetIsAttendActivity;
import com.dingding.common.core.beans.Activity;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.GetUserAttachedDTO;
import com.dingding.facde.activity.dto.AttendActivityDTO;
import com.dingding.service.BaseService;
import com.dingding.webservice.activity.IActivityService;

/**
 * @ClassName: ActivityServiceImpl
 * @Description: 活动显示Service实现类
 * @author yuHaiQing
 * @date 2016年3月29日 下午5:39:24
 */
public class ActivityServiceImpl extends BaseService implements IActivityService {
	private static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
	/**
	 * @Title: getActivityList
	 * @Description: 获取活动列表
	 * @author yuHaiQing
	 * @return List<Activity>    返回的活动列表
	 */
	@Override
	public void getActivityList(DataResponse dataResponse) {

		// 获取活动列表
		List<Activity> activities = super.activityFacade.getActivityList();

		for (Activity activity : activities) {
			activity.setPropagandaContent(null);
		}
		
		// 将获取到的优惠券信息赋值给dataResponse
		dataResponse.setResponseBody(activities);

		setStatus(dataResponse, SystemCode.SUCCESS);
	}

	/**
	 * @Title: getActivityInfo
	 * @Description: 根据活动Id获取用户详情
	 * @param reqActivityInfo 获取用户详情请求参数
	 * @param dataResponse 响应客户端的数据传输Bean
	 * @author yuHaiQing
	 * @return void    无返回值
	 */
	@Override
	public void getActivityInfo(ReqActivityInfo reqActivityInfo, DataResponse dataResponse) {

		// 判断传入产生是否为空
		if (reqActivityInfo == null || ToolUtil.isBlank(reqActivityInfo.getSnId())) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 根据活动Id获取活动详情
		Activity activity = super.activityFacade.getActivityInfo(reqActivityInfo.getSnId());

		// 将获取到的优惠券信息赋值给dataResponse
		dataResponse.setResponseBody(activity);

		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	@Override
	public void getisAttendActivity(ReqGetIsAttendActivity getIsAttendActivity, DataResponse dataResponse) {

		// 判断传入产生是否为空
		if (getIsAttendActivity == null || ToolUtil.isBlank(getIsAttendActivity.getUserId())) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		String userId = getIsAttendActivity.getUserId();

		GetUserAttachedDTO userAttachedDTO = new GetUserAttachedDTO();
		userAttachedDTO.setUserId(userId);

		int status = super.attachedFacade.isAttendActivity(userAttachedDTO);

		ResGetIsAttendActivity resGetIsAttendActivity = new ResGetIsAttendActivity();
		resGetIsAttendActivity.setStatus(status);

		dataResponse.setResponseBody(resGetIsAttendActivity);

		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	/**
	 * 
	 * @Title: attendActivity 
	 * @Description: 签到
	 * @param userId 用户ID
	 * @param type 参与活动条件
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	public BigDecimal attendActivity(String userId, int type) {
		log.info("自动取消，进入签到");
		AttendActivityDTO attendActivityDTO = new AttendActivityDTO();
		attendActivityDTO.setUserId(userId); // 用户ID
		attendActivityDTO.setAttendType(type); // 参与条件
		BigDecimal integral = userAttendActivityService.userAttendActivity(attendActivityDTO);
		
		return integral;

	}

	@Override
	public void setStatus(DataResponse dataResponse, int status) {

		String message = null;
		switch (status) {
		case SystemCode.SUCCESS:
			message = SystemCode.SUCCESS_MESSAGE;
			break;
		case SystemCode.ERROR:
			message = SystemCode.ERROR_MESSAGE;
			break;
		case SystemCode.USER_INFO_NOT_EXIST_ERROR: // 用户信息不存在
			message = SystemCode.USER_INFO_NOT_EXIST_MESSAGE;
			break;
		case SystemCode.CANCEL_ORDER_TOO_MUCH: // 取消次数超过三次
			message = SystemCode.CANCEL_ORDER_MUCH_MESSAGE;
			break;
		case SystemCode.PUBLISH_ORDER_TOO_MUCH: // 接单次数超过三次
			message = SystemCode.PUBLISH_ORDER_TOO_MUCH_MESSAGE;
			break;
		case SystemCode.DATA_NULL_ERROR: // 请求数据为空
			message = SystemCode.DATA_NULL_MESSAGE;
			break;
		case SystemCode.COUPON_ACTIVATION_FAIL:
			message = SystemCode.COUPON_ACTIVATION_FAIL_MESSAGE;// 优惠券激活失败
			break;
		case SystemCode.STORE_LOAG_NULL:
			message = SystemCode.STORE_LOAD_NULL_MESSAGE;// 使用优惠券失败
			break;
		case SystemCode.EXCHANGE_FAIL_ERROR:
			message = SystemCode.EXCHANGE_FAIL_ERROR_MESSAGE;// 兑换邀请码失败
			break;
		case SystemCode.REPEAT_EXCHANGE_ERROR:
			message = SystemCode.REPEAT_EXCHANGE_ERROR_MESSAGE;// 该兑换码对应的活动您已经参与过了
			break;
		default:
			break;
		}
		dataResponse.setResponseCode(status);
		dataResponse.setResponseMessage(message);

	}

	/**
	 * @Title: exchangeInvitationCode 
	 * @Description: 兑换邀请码
	 * @param reqExchange 请求参数
	 * @param dataResponse   响应Bean
	 * @author YHQ 
	 * @return void    返回类型
	 */
	@Override
	public void exchangeInvitationCode(ReqExchange reqExchange, DataResponse dataResponse) {
		
		if(reqExchange == null
				|| ToolUtil.isBlank(reqExchange.getUserId())
				|| ToolUtil.isBlank(reqExchange.getInvitationCode())){
			
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
		}
		
		//兑换邀请码
		Integer invitationCode = super.activityFacade.exchangeActivity(reqExchange.getUserId(), reqExchange.getInvitationCode());
		
		setStatus(dataResponse, invitationCode);
		
	}

}
